<?php

include_once "library/facil3/core/controller/file/FacilFile.php" ;
include_once "library/forum/modules/content/dao/ContentForumDAO.class.php";
/**
 * @author 		: Renato Miawaki
 * @date		: 05/12/2010
 * @version		: 1.0
 * @desc		: Controller de filens e relação com link
 * 					Retorno padrão é json, nessa versão
 *
 */
class File extends FacilFile{
	const templateResponse = "{\"success\":php[success], \"response\":\"php[response]\", \"message\":\"php[message]\"}";
	
	public function __construct($arrayRestFolder = NULL){
		//popula info
		parent::__construct($arrayRestFolder);
		$this->defaultFolderForNewFiles = Config::getFolderView("/upload/file");
		//resetando o nome do módulo para linkar para a tabela file, igual foi feita a importação
		$this->moduleName 		= "file";
		$this->defaultFile404 	= "view/forum/assets/img/no_file.gif";
		$this->defaultMinWidth				= 100;//aqui o tamanho padrão de thumb caso não seja enviado
		$this->defaultMinHeight				= 100;
    }
    public function init(){
    	$returno = parent::init();
    	echo utf8_encode($returno->toJson());
    	exit();
    }
    public function getFile(){
//    	echo Debug::li("BB iniciando get file 0 ");
    	//no caso do módulo file da loja virtual ele vai aceitar enviar content_id
    	$content_id = DataHandler::forceNumber(DataHandler::getValueByArrayIndex($this->arrayVariable, "content_id"));
    	if(count($this->arrayRestFolder) > 2 && $this->arrayRestFolder[1] == "url"){
    		//echo Debug::print_r($this->arrayRestFolder);
//    		exit();
    		//a primeira preferencia é buscar a url da file
    		$url = "";
    		for($i = 2; $i < count($this->arrayRestFolder); $i++){
    			$url .= "/".$this->arrayRestFolder[$i];
    		}
    		
    		$this->infoPost->request_file_url = $this->infoPost->file_info_url = $url;
//    		parent::getFile();
//    		exit();
    	} else if($content_id > 0){
//    		echo Debug::li("BB 2");
//    		exit();
    		$content_has_file = FALSE;
    		//mando o id da content, vai produrar a url da file
    		$ContentDAO = ContentDAO::getInstance();
    		$ContentVO = $ContentDAO->getVO();
    		if(FALSE){
    			$ContentVO = new ContentVO();
    		}
    		$returnResult 	= $ContentVO->setId($content_id, TRUE);
    		$arrayFiles 	= $ContentVO->getFile();
//    		Debug::print_r($arrayFiles);
//    		exit();
    		if($returnResult->success){
    			if(count($arrayFiles) > 0){
    				//pelo menos tem file linkada, vai retornar a primeira que achar
    				$StdFileVO = $arrayFiles[0];
    				if(file_exists($StdFileVO->url)){
    					$content_has_file = TRUE;
    					$this->infoPost->request_file_url = $this->infoPost->file_info_url  = $StdFileVO->url;
    				}
    			}
    		}
    	}// end if($content_id > 0)
//    	echo Debug::li("BB 3");
    	if($this->infoPost->request_file_url != ""){
    		//seta o id da file como zero pois nesse caso a url é 
    		$this->infoPost->request_file_id = 0;
    		
    	}
    	parent::getFile();
    }
    public function exemplo(){
    	//aqui um exemplo de como postar uma file para esse módulo
    	//veja a view. ta tudo lá.
    }
    
    public function insert(){
    	//Popula na FileInfoPost os valores enviados para registro
		if(DataHandler::getValueByArrayIndex($_POST, "file_info_type") != NULL){
    		$this->infoPost->file_info_type	= DataHandler::getValueByArrayIndex($_POST, "file_info_type");
		}
		if(DataHandler::getValueByArrayIndex($_POST, "file_info_active") != NULL){
			$this->infoPost->file_info_active = DataHandler::getValueByArrayIndex($_POST, "file_info_active");
		}
		if(DataHandler::getValueByArrayIndex($_POST, "file_info_name") != NULL){
			$this->infoPost->file_info_name = DataHandler::getValueByArrayIndex($_POST, "file_info_name");
		}
		if(DataHandler::getValueByArrayIndex($_POST, "file_info_description")){
			$this->infoPost->file_info_description = DataHandler::getValueByArrayIndex($_POST, "file_info_description");
		}
		if(DataHandler::getValueByArrayIndex($_POST, "file_info_order") != NULL){
			$this->infoPost->file_info_order = DataHandler::getValueByArrayIndex($_POST, "file_info_order");
		}
		if(DataHandler::getValueByArrayIndex($_POST, "file_info_locale") != NULL){
			$this->infoPost->file_info_locale = DataHandler::getValueByArrayIndex($_POST, "file_info_locale");
		}
		if(DataHandler::getValueByArrayIndex($_POST, "file_info_author") != NULL){
			$this->infoPost->file_info_author = DataHandler::getValueByArrayIndex($_POST, "file_info_author");
		}
		//abaixo, ATENÇÃO: Filedata
		if(DataHandler::getValueByArrayIndex($_FILES, "Filedata") != NULL){
			$this->infoPost->file_data = DataHandler::getValueByArrayIndex($_FILES, "Filedata");
		}
		//info de vinculo
		if(DataHandler::getValueByArrayIndex($_POST, "table") != NULL){
			$this->infoPost->table = DataHandler::getValueByArrayIndex($_POST, "table");
		}
		if(DataHandler::getValueByArrayIndex($_POST, "table_id") != NULL){
			$this->infoPost->table_id = DataHandler::getValueByArrayIndex($_POST, "table_id");
		}
    	$retorno = parent::insert();
    	echo utf8_encode($retorno->toJson());
    	exit();
    }
}